﻿Module modMyAppLog

    Public LogFilename As String = "logfile.htm"

    Public Sub LogEntry(ByVal argRoutineParent As String, ByVal argRoutine As String)
        LogMsg("<font color=dimgray>-- Enter " & argRoutineParent & "." & argRoutine & " --</font>")
        'Usage: LogEntry(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name)
    End Sub

    Public Sub LogExit(ByVal argRoutineParent As String, ByVal argRoutine As String)
        LogMsg("<font color=dimgray>-- Exit " & argRoutineParent & "." & argRoutine & " --</font>")
        'Usage: LogExit(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name)
    End Sub

    Public Function LogFile() As String
        Dim myEnv As MyAppEnv = New MyAppEnv
        LogFile = myEnv.MyAppDocs("Log") & "\" & LogFilename
    End Function

    Public Function LogTime() As String
        Dim xNow As Date
        xNow = Now
        LogTime = Right(Year(xNow).ToString, 2)
        LogTime = LogTime & Right("0" & Month(xNow).ToString, 2)
        LogTime = LogTime & Right("0" & DateAndTime.Day(xNow).ToString, 2)
        LogTime = LogTime & Right("0" & Hour(xNow).ToString, 2)
        LogTime = LogTime & Right("0" & Minute(xNow).ToString, 2)
        LogTime = LogTime & Right("0" & Second(xNow).ToString, 2)
    End Function

    Public Sub LogMsg(ByVal argMsg As String)
        Dim out As String = "<font color=dimgray>" & LogTime() & "</font> " & argMsg & "<br>" & vbCrLf
        IO.File.AppendAllText(LogFile(), out)
    End Sub

    Public Sub LogHR()
        Dim out As String = "<hr>" & vbCrLf
        IO.File.AppendAllText(LogFile(), out)
    End Sub

    Public Sub ViewTraceLog()
        Dim s As String = LogFile()
        If IO.File.Exists(s) Then
            Process.Start(LogFile())
        Else
            MsgBox("Log file has been deleted or file not found:" & vbCr & s, MsgBoxStyle.Exclamation)
        End If
    End Sub

    Public Sub ClearTraceLog(ByVal argConfirm As Boolean)
        Dim s As String = LogFile()
        If Not argConfirm Then
            If IO.File.Exists(s) Then IO.File.Delete(s)
        ElseIf IO.File.Exists(s) Then
            If MsgBoxResult.Yes = MsgBox("Clear trace log?", MsgBoxStyle.YesNo) Then
                IO.File.Delete(s)
                MsgBox("Trace log has been cleared.", MsgBoxStyle.Information)
            End If
        Else
            MsgBox("Log file has already been deleted or file not found:" & vbCr & s, MsgBoxStyle.Exclamation)
        End If
    End Sub

    Public Sub StartLogging()
        LogHR()
        LogMsg("<b><font color=blue>Execution started</font></b>")
    End Sub

    Public Sub StopLogging()
        LogMsg("<b><font color=blue>Execution ended</font></b>")
    End Sub
End Module
